package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.MD5Utils;
import cn.itsource.pethome.basic.util.RedisUtils;
import cn.itsource.pethome.basic.util.StrUtils;
import cn.itsource.pethome.user.constant.PhoneConstant;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDto;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserService;
import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.security.provider.MD5;

/**
 * @description: 描述:
 * @date: 2020/8/14 16:27
 * @author: summer
 * version: 1.0
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 功能描述: 注册时 电话号码验证
     * @param number
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/16
     */
    @Override
    public AjaxResult loadPhone(String number) {
        User user = userMapper.loadByPhone(number);
        if(user==null){
            return new AjaxResult();
        }
        return new AjaxResult(false,"该号码已被注册，请重新输入");
    }

    /**
     * 功能描述: 邮箱注册时，邮箱验证
     * @param email
     * @return: cn.itsource.pethome.basic.util.AjaxResult
     * @Date: 2020/8/16
     */
    @Override
    public AjaxResult loadEmail(String email) {
        User user = userMapper.loadByEmail(email);
        if(user==null){
            return new AjaxResult();
        }
        return new AjaxResult(false,"该邮箱已被注册，请重新输入");
    }

    /**
     * 前端用户注册的信息合格性验证
     * @param userDto
     */
    public void checkout(UserDto userDto){
        if(StringUtils.isEmpty(userDto.getPhone())){
            throw new RuntimeException("手机号不能为空");
        }
        if(StringUtils.isEmpty(userDto.getCode())){
            throw new RuntimeException("验证码不能为空");
        }
        if(StringUtils.isEmpty(userDto.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        if(!userDto.getConfirmPassword().equals(userDto.getPassword())){
            throw new RuntimeException("两次密码不一致");
        }
        String codephone = RedisUtils.INSTANCE.get(PhoneConstant.PHONE_REG + ":" + userDto.getPhone());
        if(StringUtils.isEmpty(codephone)){
            throw new RuntimeException("验证码过期");
        }
        String code = codephone.split(":")[0];
        if(!code.equals(userDto.getCode())){
            throw new RuntimeException("验证码错误");
        }
    }

    /**
     * 功能描述: 将usetDto转换为user对象
     * @param
     * @return: void
     * @Date: 2020/8/16
     */
    public User converting(UserDto userDto){
        User user = new User();
        BeanUtils.copyProperties(userDto,user );
        user.setUsername(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(10);
        user.setSalt(salt);
        String password = MD5Utils.encrypByMd5(user.getPassword() + salt);
        user.setPassword(password);
        user.setState(PhoneConstant.ACTIVATE);
        return user;
    }

    //用户注册
    @Override
    public void register(UserDto userDto) {
        //验证 注册信息
        checkout(userDto);
        //userDto转换为user对象
        User user = converting(userDto);
        //注册 添加
        userMapper.save(user);
    }

    /**
     * 功能描述: 前端普通用户登录验证
     * @param username 用户账号
     * @param password 用户密码
     * @return: cn.itsource.pethome.user.domain.User
     * @Date: 2020/8/16
     */
    @Override
    public User getUsernameAndPassword(String username, String password) {
        User user = userMapper.loadByPhone(username);
        if(user == null){
            throw new RuntimeException("账号错误");
        }
        if(!user.getPassword().equals(MD5Utils.encrypByMd5(password+user.getSalt()))){
            throw new RuntimeException("密码错误");
        }
        return user;
    }
}
